package com.prawn.ift.ctrlequipment.service.impl;


import com.prawn.ift.ctrlequipment.dao.CtrlequipmentButtonDao;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentButton;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentTypeState;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentButtonService;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/4/29
 * @DESC： 设备控制通道按钮管理服务
 */
@Service
public class CtrlEquipmentButtonServiceImpl implements CtrlEquipmentButtonService {

    @Autowired
    private CtrlequipmentButtonDao ctrlequipmentButtonDao;

    /**
     * 添加设备控制通道按钮
     *
     * @param ctrlEquipmentButton
     * @return Result
     */
    @Override
    public Result addCtrlEquipmentButton(CtrlequipmentButton ctrlEquipmentButton) {
        if (ctrlequipmentButtonDao.save(ctrlEquipmentButton) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除设备控制通道按钮
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteCtrlEquipmentButton(String id) {
        ctrlequipmentButtonDao.deleteById(id);
        return Result.success();

    }

    /**
     * 删除设备控制通道按钮
     *
     * @param ctrlEquipmentButton
     * @return Result
     */
    @Override
    public Result editCtrlEquipmentButton(CtrlequipmentButton ctrlEquipmentButton) {


        CtrlequipmentButton oldCtrlequipmentButton =ctrlequipmentButtonDao.findById(ctrlEquipmentButton.getId()).get();
        BeanUtils.copyProperties(ctrlEquipmentButton,oldCtrlequipmentButton, NullAttributeUtils.getNullPropertyNames(ctrlEquipmentButton));




        ctrlequipmentButtonDao.save(oldCtrlequipmentButton);
            return Result.success();

    }


    /**
     * 通过id查询按钮的详细信息
     *
     * @param id
     * @return Result
     */
    @Override
    public CtrlequipmentButton getCtrlEquipmentButtonById(String id) {


        return ctrlequipmentButtonDao.findById(id).get();
    }

    /**
     * 多条件查询
     *
     * @param ctrlEquipmentButton
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentButtonListByVo(CtrlequipmentButton  ctrlEquipmentButton) {


        List<CtrlequipmentButton> ctrlEquipmentButtonList = ctrlequipmentButtonDao.findAll(createSpecification(ctrlEquipmentButton));

        return Result.success().add(ctrlEquipmentButtonList);
    }

    /**
     * 通控制设备id查询控制按钮列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentButtonList(String ctrlEquipmentId) {

        CtrlequipmentButton ctrlequipmentButton=new CtrlequipmentButton();
        ctrlequipmentButton.setCtrlEquipmentId(ctrlEquipmentId);
        Example example=Example.of(ctrlequipmentButton);
        List<CtrlequipmentButton> ctrlEquipmentButtonList = ctrlequipmentButtonDao.findAll(example);

        return Result.success().add(ctrlEquipmentButtonList);
    }

    /**
     * 通过设备控制通道id查询按钮的列表
     * @param relId
     * @return
     */
    @Override
    public Result getCtrlEquipmentButtonListByRelId(String relId) {
        CtrlequipmentButton ctrlequipmentButton=new CtrlequipmentButton();
        ctrlequipmentButton.setRelId(relId);
        Example example=Example.of(ctrlequipmentButton);

        return Result.success().add(ctrlequipmentButtonDao.findAll(example));
    }

    private Specification<CtrlequipmentButton> createSpecification(CtrlequipmentButton ctrlequipmentButton) {

        return new Specification<CtrlequipmentButton>() {

            @Override
            public Predicate toPredicate(Root<CtrlequipmentButton> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (ctrlequipmentButton.getId()!=null && !"".equals(ctrlequipmentButton.getId())) {
                    predicateList.add(cb.equal(root.get("id").as(String.class), ctrlequipmentButton.getId()));
                }
                // 真实通道id
                if (ctrlequipmentButton.getRelId()!=null && !"".equals(ctrlequipmentButton.getRelId())) {
                    predicateList.add(cb.equal(root.get("relId").as(String.class),ctrlequipmentButton.getRelId() ));
                }
                // 通道号
                if (ctrlequipmentButton.getChannelNo()!=null && !"".equals(ctrlequipmentButton.getChannelNo())) {
                    predicateList.add(cb.equal(root.get("channelNo").as(String.class),ctrlequipmentButton.getChannelNo() ));
                }
                // 按钮名称
                if (ctrlequipmentButton.getButtonName()!=null && !"".equals(ctrlequipmentButton.getButtonName())) {
                    predicateList.add(cb.equal(root.get("buttonName").as(String.class), ctrlequipmentButton.getButtonName()));
                }
                // 操作类型（0关1开）
                if (ctrlequipmentButton.getOperationType()!=null && !"".equals(ctrlequipmentButton.getOperationType())) {
                    predicateList.add(cb.equal(root.get("operationType").as(String.class), ctrlequipmentButton.getOperationType()));
                }
                // 控制类型
                if (ctrlequipmentButton.getCtrlType()!=null && !"".equals(ctrlequipmentButton.getCtrlType())) {
                    predicateList.add(cb.equal(root.get("ctrlType").as(String.class), ctrlequipmentButton.getCtrlType()));
                }
                // 所属控制设备编号
                if (ctrlequipmentButton.getCtrlEquipmentId()!=null && !"".equals(ctrlequipmentButton.getCtrlEquipmentId())) {
                    predicateList.add(cb.equal(root.get("ctrlEquipmentId").as(String.class),ctrlequipmentButton.getCtrlEquipmentId() ));
                }
                // 时间单位
                if (ctrlequipmentButton.getTimeUnit()!=null && !"".equals(ctrlequipmentButton.getTimeUnit())) {
                    predicateList.add(cb.equal(root.get("timeUnit").as(String.class),ctrlequipmentButton.getTimeUnit() ));
                }
                // 
                if (ctrlequipmentButton.getRemarks()!=null && !"".equals(ctrlequipmentButton.getRemarks())) {
                    predicateList.add(cb.equal(root.get("remarks").as(String.class), ctrlequipmentButton.getRemarks()));
                }
                // 
                if (ctrlequipmentButton.getCreateBy()!=null && !"".equals(ctrlequipmentButton.getCreateBy())) {
                    predicateList.add(cb.equal(root.get("createBy").as(String.class), ctrlequipmentButton.getCreateBy()));
                }
                // 
                if (ctrlequipmentButton.getUpdateBy()!=null && !"".equals(ctrlequipmentButton.getUpdateBy())) {
                    predicateList.add(cb.equal(root.get("updateBy").as(String.class), ctrlequipmentButton.getUpdateBy()));
                }
                // 
                if (ctrlequipmentButton.getDelFlag()!=null && !"".equals(ctrlequipmentButton.getDelFlag())) {
                    predicateList.add(cb.equal(root.get("delFlag").as(String.class), ctrlequipmentButton.getDelFlag()));
                }
                // 所属设备编号
                if (ctrlequipmentButton.getEquipmentId()!=null && !"".equals(ctrlequipmentButton.getEquipmentId())) {
                    predicateList.add(cb.equal(root.get("equipmentId").as(String.class),ctrlequipmentButton.getEquipmentId() ));
                }
                // 操作延时
                if (ctrlequipmentButton.getOperationDelay()!=null && !"".equals(ctrlequipmentButton.getOperationDelay())) {
                    predicateList.add(cb.equal(root.get("operationDelay").as(String.class), ctrlequipmentButton.getOperationDelay()));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }


}
